package Dao;

import Entity.Selection;
import Mybatis.Mybatis;

import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class SelectionDao implements SelectionDaoImpl {
    public List<Selection> findAllSelection() {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.SelectionMapper.findAllSelection";
        List<Selection> selections = session.selectList(statement);
        session.close();
        return selections;
    }
    public Selection findById(int id) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.SelectionMapper.findById";
        Selection selection = session.selectOne(statement, id);
        session.close();
        return selection;
    }
    public List<Selection> findByStu_number(String stu_number) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.SelectionMapper.findByStu_number";
        List<Selection> selections = session.selectList(statement, stu_number);
        session.close();
        return selections;
    }
    public List<Selection> findByEx_id(int ex_id) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.SelectionMapper.findByEx_id";
        List<Selection> selections = session.selectList(statement, ex_id);
        session.close();
        return selections;
    }
    public List<Selection> findByIsreport(int isreport) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.SelectionMapper.findByIsreport";
        List<Selection> selections = session.selectList(statement, isreport);
        session.close();
        return selections;
    }
    public List<Selection> findByTimeA(String roomid, int week, int day, int section) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        Selection selection = new Selection(null, null, 0, null, week, day, section, roomid);
        String statement = "Mapper.SelectionMapper.findByTimeA";
        List<Selection> selections = session.selectList(statement, selection);
        session.close();
        return selections;
    }
    public boolean addSelection(Selection selection) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.SelectionMapper.addSelection";
        int count = session.insert(statement, selection);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }
    public boolean updateSelection(Selection selection) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.SelectionMapper.updateSelection";
        int count = session.update(statement, selection);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }
    public boolean deleteSelection(Selection selection) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.SelectionMapper.deleteSelection";
        int count = session.delete(statement, selection);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }
    public boolean deleteOne(String stu_number, int ex_id) {
    	Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        Selection selection = new Selection(stu_number, null, ex_id, null);
        String statement = "Mapper.SelectionMapper.deleteOne";
        int count = session.update(statement, selection);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }

    // ����
    public static void main(String[] args) {
        SelectionDao test = new SelectionDao();

        Selection selection = new Selection("618", "С��", 4, "ʵ����");
        test.addSelection(selection);
        selection.setId(8);
        selection.setWeek(10);
        selection.setDay(2);
        selection.setSection(2);
        selection.setRoomid("1001");
        test.updateSelection(selection);
        test.deleteSelection(selection);
        List<Selection> selections = test.findAllSelection();
        for(int i = 0; i < selections.size(); i++) {
            System.out.println(selections.get(i).getId()+" "+selections.get(i).getStu_number()+" "+selections.get(i).getStu_name()+" "+
                   selections.get(i).getEx_id()+" "+selections.get(i).getEx_name());
        }

        /*
        Selection selection = test.findById(2);
        System.out.println(selection.getId()+" "+selection.getStu_number()+" "+selection.getStu_name()+" "+
                selection.getEx_id()+" "+selection.getEx_name());
         */
        /*
        List<Selection> selections = test.findByStu_number("618");
        for(int i = 0; i < selections.size(); i++) {
            System.out.println(selections.get(i).getId()+" "+selections.get(i).getStu_number()+" "+selections.get(i).getStu_name()+" "+
                    selections.get(i).getEx_id()+" "+selections.get(i).getEx_name());
        }
         */
        /*
        List<Selection> selections = test.findByEx_id(4);
        for(int i = 0; i < selections.size(); i++) {
            System.out.println(selections.get(i).getId()+" "+selections.get(i).getStu_number()+" "+selections.get(i).getStu_name()+" "+
                    selections.get(i).getEx_id()+" "+selections.get(i).getEx_name());
        }
         */
        /*
        List<Selection> selections = test.findByIsreport(1);
        for(int i = 0; i < selections.size(); i++) {
            System.out.println(selections.get(i).getId()+" "+selections.get(i).getStu_number()+" "+selections.get(i).getStu_name()+" "+
                    selections.get(i).getEx_id()+" "+selections.get(i).getEx_name());
        }
         */
        /*
        List<Selection> selections = test.findByTimeA("1002", 8, 4, 3);
        for(int i = 0; i < selections.size(); i++) {
            System.out.println(selections.get(i).getId()+" "+selections.get(i).getStu_number()+" "+selections.get(i).getStu_name()+" "+
                    selections.get(i).getEx_id()+" "+selections.get(i).getEx_name());
        }
         */
    }
}
